﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LinQ
{
    class TestTheTask
    {
        static void Main(string[] args)
        {
            List<Group> groups = new List<Group>();
            groups.Add(new Group() { Id = 1, Name = "Mathematics" });
            groups.Add(new Group() { Id = 2, Name = "Informatics" });

            List<Student> students = new List<Student>();
            students.Add(new Student
            {
                FirstName = "Pesho",
                LastName = "Petrov",
                GroupId = 2,
                Email = "pesho@abv.bg",
                Marks = new List<int> { 2, 2, 6 },
                FN = "1111106"
            });
            students.Add(new Student
            {
                FirstName = "Ilian",
                LastName = "Stefanov",
                GroupId = 2,
                Email = "ilian@abv.bg",
                Marks = new List<int> { 2, 3, 3 },
                FN = "1211105"
            });
            students.Add(new Student
            {
                FirstName = "Ivo",
                LastName = "Stefanov",
                GroupId = 1,
                Email = "Ivo@yahoo.com",
                Marks = new List<int> { 4, 4, 4 },
                FN = "1311107"
            });
            students.Add(new Student
            {
                FirstName = "Sasho",
                LastName = "Pramatarov",
                GroupId = 2,
                Email = "sasho@abv.bg",
                Marks = new List<int> { 5, 5, 6 },
                FN = "1311106"
            });

            #region tast 1
            //var selection =
            //    from s in students
            //    where s.GroupId == 2
            //    select s;

            //foreach (var item in selection)
            //{
            //    Console.WriteLine("FirstName: {0}, Group: {1}",item.FirstName, item.GroupId);
            //}
            //Console.WriteLine("\n\n");

            //var sel = students.Where(x => x.GroupId == 2).Where(x=>x.Email.Contains("abv.bg"));

            //foreach (var item in sel)
            //{
            //    Console.WriteLine("FirstName: {0}, Group: {1}", item.FirstName, item.GroupId);
            //}
            #endregion

            #region task 2

            //var sel = students.Where(x => x.Email.Contains("abv.bg"));
            //foreach (var item in sel)
            //{
            //    Console.WriteLine("FirstName: {0}, Group: {1}", item.FirstName, item.GroupId);
            //}
            #endregion

            #region task 5

            //var sel = students.Where(x => x.Marks.Contains(6));
            //foreach (var item in sel)
            //{
            //    Console.WriteLine("FirstName: {0}, Group: {1}", item.FirstName, item.GroupId);
            //}
            #endregion

            #region task 6

            //var sel = students.Where(student => student.Marks.Count(x => x == 2) == 2);
            //foreach (var itemA in sel)
            //{
            //    Student item = itemA as Student;
            //    if (item != null)
            //    {
            //        Console.WriteLine("FirstName: {0}, Group: {1}", item.FirstName, item.GroupId);
            //    }
            //}
            #endregion

            #region task 7

            //var sel = students.Where(student => student.FN.Substring(5,2)== "06");
            //foreach (var itemA in sel)
            //{
            //    Student item = itemA as Student;
            //    if (item != null)
            //    {
            //        Console.WriteLine("FirstName: {0}, Group: {1}", item.FirstName, item.GroupId);
            //    }
            //}
            #endregion

            #region task 8

            var sel = students.Join(groups, f => f.GroupId, s => s.Id, (st, gr) =>
                new { StudentName = st.FirstName, GroupName = gr.Name }).
                Where(x => x.GroupName == "Mathematics");
            foreach (var itemA in sel)
            {
                Console.WriteLine("FirstName: {0}, Group: {1}", itemA.StudentName, itemA.GroupName);
            }
            #endregion
        }
    }
}
